package com.example.work4.http

import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.example.work4.LoginActivity
import com.example.work4.TestActivity
import com.example.work4.common.FIN
import com.example.work4.data.MyObServerable

import com.example.work4.http.imp.SSORetrofit
import com.example.work4.po.*
import okhttp3.MediaType
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class SSORetrofitImp(private var context: Context) {

    private val retrofit= BaseRetrofit.get(FIN.BaseUrl)

    private val thisRetro= retrofit.create(SSORetrofit::class.java);


    fun sendEmail(address:String) {
        thisRetro.sendEmail(address).enqueue(object : Callback<ResultBean<Void>> {
            override fun onResponse(call: Call<ResultBean<Void>>, response: Response<ResultBean<Void>>) {
                Toast.makeText(context,"发送完毕",Toast.LENGTH_SHORT).show()
            }

            override fun onFailure(call: Call<ResultBean<Void>>, t: Throwable) {
                Toast.makeText(context,"发送失败",Toast.LENGTH_SHORT).show()
            }
        })
    }

    fun register(info: RegisterInfo) {
        thisRetro.register(info).enqueue(object : Callback<ResultBean<UserInfo>> {
            override fun onResponse(call: Call<ResultBean<UserInfo>>, response: Response<ResultBean<UserInfo>>) {
                if (response.code() == 200) {
                    Toast.makeText(context, "注册完毕", Toast.LENGTH_SHORT).show()
                    val intent = Intent();
                    intent.setClass(context, LoginActivity::class.java)
                    context.startActivity(intent)
                }else{
                    Toast.makeText(context,"注册失败",Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<UserInfo>>, t: Throwable) {
                Toast.makeText(context,"注册失败",Toast.LENGTH_SHORT).show()
            }

        })
    }

    fun login(username:String,password:String) {
        val it = RequestBody.create(MediaType.parse("text/plain"), username)
        val it1 = RequestBody.create(MediaType.parse("text/plain"), password)
        thisRetro.send(it,it1)
            .enqueue(object : Callback<ResultBean<UserInfo>> {
                override fun onFailure(
                    call: retrofit2.Call<ResultBean<UserInfo>>,
                    t: Throwable
                ) {
                    Toast.makeText(context, "网络故障，请稍后再试", Toast.LENGTH_SHORT).show()

                }
                override fun onResponse(
                    call: retrofit2.Call<ResultBean<UserInfo>>,
                    response: Response<ResultBean<UserInfo>>
                ) {
                    var code = response.body()?.code
                    response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                    if (code == 0) {
                        SSOInfo.token = response.body()?.data?.token
                        SSOInfo.avatar =response.body()?.data?.avatar
                        SSOInfo.email =response.body()?.data?.email
                        SSOInfo.username = response.body()?.data?.name
                        SSOInfo.id = response.body()?.data?.id
                        SSOInfo.isLogin=true
                        /**初始化头像**/
                        val intent = Intent(context, TestActivity::class.java)
                        context.startActivity(intent)
                    } else {
                        Toast.makeText(context, "登陆失败，请检查账号密码！", Toast.LENGTH_SHORT).show()
                    }
                }
            }
            )
    }
}